home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12152 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  55 lines

  1. Path: news.compuserve.com!newsmaster
  2. From: 76623,2065@compuserve.com  (Bobby Martin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: HELP: Algorithm for ordering (x,y) coordinates
  5. Date: 18 Mar 1996 17:03:41 GMT
  6. Organization: CompuServe Incorporated
  7. Message-ID: <4ik51d$euq@dub-news-svc-4.compuserve.com>
  8. References: <314B0266.456A@mit.edu>
  9. Reply-To: 76623,2065@compuserve.com (Bobby Martin)
  10. NNTP-Posting-Host: ad51-239.compuserve.com
  11. X-Newsreader: IBM NewsReader/2 v1.03
  12.  
  13. In <314B0266.456A@mit.edu>, Imran Haq <ihaq@mit.edu> writes:
  14. >Here is a problem that I can't seem to express in code easily using STL:
  15. ..
  16. <snip>
  17. ..
  18. >requires only two arguments, not the 3 I need to compute order. In fact, what
  19. >I really need is to have this custom function automatically accept the 
  20. >first coordinate pair as a reference point, and modify the order of the 2nd and 3rd
  21. >points if they are not anticlockwise. 
  22. >
  23. >Any suggestions? Or do I need to write a sort algorithm outside what STL provides?
  24. >
  25. >
  26. >Imran Haq
  27. >ihaq@mit.edu
  28.  
  29. Try writing a compare function for the points that accepts the two points
  30. to be swapped.  Then set up two distinct point variables from your set to
  31. be used as pivot points in your compare function.  Inside the compare
  32. function, find one of the two pivots that is not equal to either of the points
  33. passed into the compare function, and use is as the third point to find the
  34. vectors for your cross product.
  35.  
  36. PS: I advise using a class with two static member variables as your repository
  37. for the two pivot points, like so:
  38.  
  39. class PointHolder {
  40. private:
  41.     Point a;
  42.     Point b;
  43. public:
  44.     Point& a() {return a;}
  45.     Point& b() {return b;}
  46.     void a(const Point& newA) {a=newA;}
  47.     void b(const Point& newB) {b=newB;}
  48.     };
  49.  
  50. Hope that helps!
  51.  
  52. Bobby Martin
  53.  
  54. Feel free to email me if anything is unclear.
  55.